package cn.uestc.labhms.modules.hms.service.impl;

import cn.uestc.labhms.modules.hms.exception.HMSException;
import cn.uestc.labhms.modules.hms.service.UserService;
import cn.uestc.labhms.modules.sys.dao.SysUserDao;
import cn.uestc.labhms.modules.sys.entity.SysUserEntity;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements UserService {
    @Override
    public void register(SysUserEntity sysUserEntity) {
        String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUserEntity.setSalt(salt);
        String password = new Sha256Hash(sysUserEntity.getPassword(), salt).toHex();
        sysUserEntity.setPassword(password);
        sysUserEntity.setCreateTime(new Date());
        sysUserEntity.setStatus(1);
        boolean save = this.save(sysUserEntity);
        // 注册失败
        if (!save) {
            throw new HMSException("由于未知原因，注册失败");
        }
    }
}
